#include "ObjectManager.h"
#include "CBase.h"

ObjectManager* ObjectManager::pInstance;

ObjectManager::ObjectManager( void )
{
}
ObjectManager::~ObjectManager( void )
{
}
ObjectManager* ObjectManager::GetInstance( void )
{
	if(!pInstance)
		pInstance = new ObjectManager;

	return pInstance;
}
void ObjectManager::DeleteInstance( void )
{
	if( pInstance )
	{
		delete pInstance;
		pInstance = NULL;
	}
}
void ObjectManager::UpdateObjects( float fElapsedTime )
{ 
	//vector<CBase*>::iterator iter = m_vObjects.begin();
	
	for(unsigned int i = 0; i < m_vObjects.size(); ++i)
		m_vObjects[i]->Update(fElapsedTime);
}
void ObjectManager::RenderObjects( void )
{
	//vector<CBase*>::iterator iter = m_vObjects.begin();
	
	for(unsigned int i = 0; i < m_vObjects.size(); ++i)
		m_vObjects[i]->Render();
}
void ObjectManager::AddObject( CBase* pBase )
{
	if( pBase == NULL )
		return;

	m_vObjects.push_back( pBase );

	pBase->AddRef();
}
void ObjectManager::RemoveObject( CBase* pBase )
{
	if( pBase == NULL )
		return;

	for( vector<CBase*>::iterator iter = m_vObjects.begin() ;
			iter != m_vObjects.end() ; 
			++iter )
	{
		if( (*iter) == pBase )
		{
			(*iter)->Release();
			iter = m_vObjects.erase( iter );
			break;
		}
	}

}
void ObjectManager::RemoveAllObjects( void )
{
	for( UINT i = 0 ; i < m_vObjects.size() ; ++i )
	{
		m_vObjects[ i ]->Release();
	}
	m_vObjects.clear();
}
bool ObjectManager::CheckCollision( void )
{
	for( UINT i = 0 ; i < m_vObjects.size() ; ++i  )
	{
		for( UINT j = 0 ; j < m_vObjects.size(); ++j )
		{
			if( m_vObjects[ i ]->GetType() == m_vObjects[ j ]->GetType() )
				continue;
			if( m_vObjects[ i ]->CheckCollision( m_vObjects[ j ] ) )
				break;
		}
	}

	return true;
}
